package base.search;

import java.util.PrimitiveIterator;

public class BinarySearch {

    /**
     * 二分查找
     * @param nums
     * @param target
     * @return
     */
    public static int search (int[] nums, int target) {
        int left = 0,   right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) return mid;
            if (nums[mid] > target) right = mid - 1;
            else left = mid + 1;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] a = {-1,0,3,4,6,10,13,14};
        System.out.println(search(a,13));

    }

}
